﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Telerik.WebControls;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class InstrumentCategoryList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GenerateTreeView();
        }
        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            GenerateTreeView();
        }

        private void GenerateTreeView()
        {
            this.RadTreeView1.Nodes.Clear();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();

            List<CtrtInstrumentCategoryEntity> categoryEntitys = new CtrtInstrumentCategoryBll().GetAll<CtrtInstrumentCategoryEntity>(parms,"Order By CategoryCode");

            List<CtrtInstrumentCategoryEntity> subCategoryEntitys = categoryEntitys.FindAll(delegate(CtrtInstrumentCategoryEntity e) { return e.parentGUID == Guid.Empty; });
            foreach (CtrtInstrumentCategoryEntity ent in subCategoryEntitys)
            {
                RadTreeNode node = CreateNode(ent.categoryCode+ent.categoryName, ent.categoryName + ent.categoryCode, false, ent.categoryGUID.ToString());
                RadTreeView1.Nodes.Add(node);

                RecursivelyPopulate(categoryEntitys, node);
            }
        }

        private RadTreeNode CreateNode(string text, string toolTip, bool expanded, string id)
        {
            RadTreeNode node = new RadTreeNode(text, id);
            node.Expanded = expanded;
            node.ContextMenuName = "Management";
            return node;
        }

        private void RecursivelyPopulate(List<CtrtInstrumentCategoryEntity> cateList, RadTreeNode node)
        {
            List<CtrtInstrumentCategoryEntity> SubDeptList = cateList.FindAll(delegate(CtrtInstrumentCategoryEntity e) { return e.parentGUID == new Guid(node.Value); });
            foreach (CtrtInstrumentCategoryEntity ent in SubDeptList)
            {
                RadTreeNode radTreeNode = CreateNode(ent.categoryCode+ent.categoryName , ent.categoryName + ent.categoryCode, false, ent.categoryGUID.ToString());
                node.Nodes.Add(radTreeNode);
                RecursivelyPopulate(cateList, radTreeNode);
            }
        }

        protected void RadTreeView1_NodeClick(object o, RadTreeNodeEventArgs e)
        {
            ContractBll bll = new ContractBll();
            string category = new Guid(e.NodeClicked.Value).ToString();

            List<CtrtInstrumentEntity> list = bll.GetInstrumentEntities(new Guid(e.NodeClicked.Value));
            InstrumentList.DataSource = list;
            InstrumentList.DataBind();
            if (bll.TreeNodeIsLeaf(category) > 0)
            {
                hiddenCategoryID.Value = category;
               
            }
        }

       
    }
}
